<template>
	<div class="situation">
		<div class="title">
			<div class="title_dishName"><text>菜品名称</text></div>
			<div class="title_amt"><text>金额(元)</text></div>
			<div class="title_dishName"><text>健康禁忌</text></div>
			<div class="title_amt"><text>选择</text></div>
		</div>
		<div class="Principallist" v-for="(item,index) in dataCode.details " :key="index">
			<div class="list_dishName"><text>{{item.dishName}}</text></div>
			<div class="list_amt"><text>{{item.amt}}</text></div>
			<div class="list_dishName"><text>{{item.labProd}}</text></div>
			<div class="list_amt">
				<checkbox @click="change(item,index)" :checked="item.checked" />
			</div>

		</div>
		<div class="money">
			<div class="moneys"><text>合计退款: {{money}} 元</text></div>
		</div>
		<view class="uni-btn-v">
			<button form-type="submit" type="primary" @click="disarmTheAlarm()">提交</button>
		</view>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				money: 0,
				dataCode: [],
				detailss: [],
			}
		},
		onLoad: function(e) {
			let obj = e.dataCode.replace("\"([^\"]*)\"", "$1");
			this.dataCode = JSON.parse(obj)
			for (let i = 0; i < this.dataCode.details.length; i++) {
				this.$set(this.dataCode.details[i], 'checked', false)
			}
			console.log(this.dataCode)
		},
		methods: {
			change: function(item, index) {
				item.checked = !item.checked
				if (item.checked == true) {
					this.detailss.push(item)
					this.money = item.amt + this.money
				} else {
					this.money = this.money - item.amt
					for (var i = 0; i < this.detailss.length; i++) {
						// 严格比较，即类型与数值必须同时相等。
						if (this.detailss[i] === item) {
							this.detailss.splice(i, 1);
							// this.dataCode.details.splice(i, 1);
							// break;
						}
					}
				}
				// console.log( item)
				console.log(this.detailss)

			},
			async disarmTheAlarm() {
				const res = await this.$ssgwRequest({
					url: '/api-baiyun/cancelOrder/save',
					method: "post",
					data: {
						cancelAmt:this.money,
						seq:this.dataCode.seq,
						id:this.dataCode.id,
						details:this.detailss
					}
				})
				if (res.success == true) {
					console.log(res)
					uni.showToast({
						title: '您退单成功!',
						duration: 2000
					});
					// 调用上一个页面方法
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2];
					// #ifdef H5
					prevPage.parentFun(this.dataCode,this.detailss)
					// #endif
					// #ifndef H5
					prevPage.$vm.parentFun(this.dataCode,this.detailss)
					// #endif
					console.log(pages)
					uni.navigateBack()
					// uni.redirectTo({
					//     url: '/pages/callThePolice/menuAlarm/index'
					// });
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.situation {
		font-size: 30rpx;
	}

	.title {
		display: flex;
		height: 60rpx;
		text-align: center;
		align-items: center;
		border-bottom: 1px solid #000000;

		.title_dishName {
			width: 35%;
		}

		.title_amt {
			width: 15%;
		}
	}

	.Principallist {
		display: flex;
		text-align: center;
		align-items: center;
		height: 80rpx;
		border-bottom: 1px solid #000000;

		.list_dishName {
			// border: 1px solid #000000;
			width: 35%;
			// background-color: #007AFF;
		}

		.list_amt {
			// border: 1px solid #000000;
			width: 15%;
		}
	}

	.money {
		margin-top: 20rpx;
		height: 80rpx;
	}

	.uni-btn-v {
		width: 80%;
		height: 40rpx;
		margin: 50rpx auto;
		border-radius: 10px;
	}
</style>
